package com.cheng.student.controller.servlet;

import com.cheng.student.service.TeacherServiceImpl;
import com.cheng.student.service.interfaces.TeacherService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "GetTeachersServlet", value = "/super/GetTeachersServlet")
public class GetTeachersServlet
        extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String starts = request.getParameter("start");
        String ends = request.getParameter("end");

        //校验
        if (starts == null|| ends == null
        || "".equals(starts)|| "".equals(ends))
        {
            response.getWriter().write("[]");
            return;
        }
        int start ,end;
        try
        {
            start = Integer.parseInt(starts);
            end = Integer.parseInt(ends);
        }
        catch (NumberFormatException e)
        {
            response.getWriter().write("[]");
            return;
        }
        if (start < 0 || end < 0 || end < start)
        {
            response.getWriter().write("[]");
            return;
        }
        //通过
        TeacherService service = new TeacherServiceImpl();

        String teachersJson = service.getTeachersJson(start, end);

        response.getWriter().write(teachersJson);
    }
}
